app.alert=function () {
    //变量定义
    var SHOW_TYPE = {
            SUCCESS: 'success',
            _DEFAULT: 'info',
            ERROR: 'pink',
            WARNING: 'warning',
            PINK: 'pink',
            MESSAGE: 'message'
        },
        alertCtnTemp = '<ul id="alertList" data-role="alertList" class="alert-list unstyled" style="z-index: 8;"></ul>',
        alertCttTemp = '<li data-alert-id="_id_" class="alert-_showType_"><div class="alert-container"><div class="alert-content" title="_content_">_content_</div></div><button type="button" class="close alert-close">&times;</button></li>',


        alertQueueLength = Math.max(Math.ceil($(window).height() / 100), 3),
        alertQueue = [],
        type,

        $alert = $('#alertList'),


        messageDialog = function () {
            var queen = [],
                clickHandler = function () {
                    var msg, result = true;
                    queen.shift();

                    if (queen.length) {
                        msg = queen[0];
                        while (queen.length && !msg) {
                            queen.shift();
                            msg = queen[0];
                        }
                        if (msg) {
                            $(this).find('[data-role=message]').html(msg.toString().replace(/\n/g, '<br/>'));
                            result = false;
                        }
                    }
                    return result;
                };

            return function (msg) {
                var modal;
                queen.push(msg);

                if (queen.length === 1 && (msg = queen[0])) {

                    modal = app.modal || window.app && window.app.modal || function (option) {
                        app.alert(option.content);

                        clickHandler();
                    };

                    modal({
                        title: '信息提示',
                        btnCancel: '关闭',
                        confirmHandler: clickHandler,
                        cancelHandler: clickHandler,
                        content: '<div class="aui-ide-modal-content"><i class="iconfont icon-round_warming"></i><p data-role="message">' + msg.toString().replace(/\n/g, '<br/>') + '</p></div>',
                        isDialog: true,
                        isLargeModal: false,
                        init: function () {
                            var $body = $(this);
                            setTimeout(function () {
                                $body.prev().find('.close').off().remove();
                            }, 100);
                        }
                    });
                }
            }
        }(),
        messageGooflow = function () {
            var queen = [],
                clickHandler = function () {
                    var msg, result = true;
                    queen.shift();

                    if (queen.length) {
                        msg = queen[0];
                        while (queen.length && !msg) {
                            queen.shift();
                            msg = queen[0];
                        }
                        if (msg) {
                            $(this).find('[data-role=message]').html(msg.toString().replace(/\n/g, '<br/>'));
                            result = false;
                        }
                    }
                    return result;
                };

            return function (msg) {
                var modal;
                queen.push(msg);

                if (queen.length === 1 && (msg = queen[0])) {

                    modal = app.modal || window.app && window.app.modal || function (option) {
                        app.alert(option.content);

                        clickHandler();
                    };

                    modal({
                        title: '信息详情',
                        btnCancel: '关闭',
                        confirmHandler: clickHandler,
                        cancelHandler: clickHandler,
                        content: '<div class="aui-ide-modal-content"><i class="iconfont icon-round_warming"></i><p data-role="message">' + msg.toString().replace(/\n/g, '<br/>') + '</p></div>',
                        isDialog: true,
                        isLargeModal: false,
                        init: function () {
                            var $body = $(this);
                            setTimeout(function () {
                                $body.prev().find('.close').off().remove();
                            }, 100);
                        }
                    });
                }
            }
        }(),

        addToQueue = function (args) {
            var i, item, id,
                result = false;

            if (id = args[2]) {
                for (i = -1; item = alertQueue[++i];) {
                    if (result = (item[2] === id)) {
                        break;
                    }
                }
            }

            if (!result) {
                alertQueue.push(args);
            }
        },
        delFormQueue = function () {
            return alertQueue.shift();
        },
        execAlert = function (msg, type, id) {
            var $item = null;

            //校验样式在_showType中
            type = type || 'info';
            type = (type.toUpperCase() in SHOW_TYPE) ? type : SHOW_TYPE._DEFAULT;


            if (type === SHOW_TYPE.MESSAGE) {
                messageDialog(msg);
                console.info(msg);
            } else {
                if ($alert.children().length < alertQueueLength) {
                    if (!id || !$alert.children('[data-alert-id="' + id + '"]').length) {
                        $item = $alert
                            .append(alertCttTemp.replace(/_id_/, id).replace(/_showType_/, type).replace(/_content_/g, msg))
                            .children(':last');

                        //出现
                        setTimeout(function () {
                            $item.addClass('out');
                        }, 50 + Math.random() * 50);
                        //隐藏

                        setTimeout(function () {
                            $item.animate({
                                top: '-1.5em',
                                opacity: 0,
                                height: 0
                            });
                            execNextAlert($item);
                        }, 10000 + Math.random() * 1000);
                    }
                } else {
                    addToQueue(arguments);
                }
            }
        },
        execNextAlert = function ($lastElem) {
            setTimeout(function () {
                if ($lastElem) {
                    $lastElem.remove();
                    $lastElem = null;
                }
                if (alertQueue.length) {
                    execAlert.apply(this, delFormQueue());
                }
            }, 500);
        },
        alertFunc = function (msg, showType, id) {
            if (msg instanceof Array) {
                for (var i = -1, alt; alt = msg[++i];) {
                    if (alt instanceof Array) {
                        execAlert(alt[0], alt[1], alt[2]);
                    } else {
                        execAlert(alt, showType, id);
                    }
                }
            } else {
                execAlert(msg, showType, id);
            }
        };

    //初始化数据
    if (!$alert.length) {
        $alert = $(alertCtnTemp);
        $alert.appendTo('body');
    }

    for (type in SHOW_TYPE) {
        if (SHOW_TYPE.hasOwnProperty(type)) {
            alertFunc[type] = SHOW_TYPE[type];
        }
    }

    //override alert
    window.alert = messageDialog;

    /*监听绑定*/
    //关闭按钮
    $alert.click(function (e) {
        e = e.target || window.event.srcElement;
        if (e.className.indexOf('close') >= 0) {
            $(e).parent().removeClass('out').animate({
                height: 0
            });
            execNextAlert($(e).parent());
        }
    });

    /*详情请见api部分*/
    return alertFunc;
}()